define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'order/index' + location.search,
                    add_url: 'order/add',
                    edit_url: 'order/edit',
                    // del_url: 'order/del',
                    multi_url: 'order/multi',
                    import_url: 'order/import',
                    table: 'order',
                }
            });


            var table = $("#table");

            //当表格数据加载完成时
            table.on('load-success.bs.table', function (e, data) {
                //这里可以获取从服务端获取的JSON数据
                console.log(data);
                //这里我们手动设置底部的值
                $("#money").text(data.extend.money);
            });

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                fixedColumns: true,
                fixedRightNumber: 1,
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('Id'), operate: false},
                        {field: 'username', title: __('Username'), operate: 'LIKE'},
                        {field: 'name', title: __('商品名称')},
                        {field: 'title', title: __('规格'), operate: false},
                        {field: 'num', title: __('数量'), operate:  'LIKE'},
                        {field: 'money', title: __('Money'), operate: false},
                        {field: 'type', title: __('Type'),searchList: {1: __('微信支付'),2: __('余额支付'),3:__('积分')}},
                        {field: 'uaname', title: __('姓名')},
                        {field: 'address', title: __('详细地址'),operate: false},
                        {field: 'number', title: __('联系方式')},
                        {field: 'status', title: __('Status'),
                            searchList: {1: __('待支付'),2: __('待发货'),3:__('待收货'),4:__('退款中'),5:__('已完成'),6:__('已退款')}},
                        {field: 'create_time', title: __('Create_time'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
                        {
                            field: 'operate',
                            width: "150px",
                            title: __('Operate'),
                            table: table,
                            events: Table.api.events.operate,
                            buttons: [
                                {
                                    name: '退款',
                                    text: "退款",
                                    title: '退款',
                                    classname: 'btn btn-xs btn-success btn-magic btn-ajax',
                                    icon: 'fa fa-list',
                                    url: 'order/desc',
                                    visible:function (row){
                                        if (row.status == '退款中'){
                                            return true;
                                        }
                                        return false;
                                    },
                                    callback: function (data) {
                                        Layer.alert("接收到回传数据：" + JSON.stringify(data), {title: "回传数据"});
                                    }
                                },
                                {
                                    name: 'edit',
                                    text: "发货",
                                    title: __('Edit'),
                                    classname: 'btn btn-xs btn-success btn-editone',
                                    icon: 'fa fa-pencil',
                                    url: 'order/desc',
                                    visible:function (row){
                                        if (row.status == '待发货'){
                                            return true;
                                        }
                                        return false;
                                    }
                                }
                            ],
                            formatter: Table.api.formatter.operate
                        },
                        // {
                        //     field: 'operate',
                        //     title: __('Operate'),
                        //     table: table,
                        //     events: Table.api.events.operate,
                        //     formatter: Table.api.formatter.operate
                        // }
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        add: function () {
            Controller.api.bindevent();
        },
        edit: function () {
            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            }
        }
    };
    return Controller;
});
